<?php 
class ControllerProductSpecial extends Controller {  
	//tuan
	function dateDiff($time1, $time2, $precision = 6) {
		// If not numeric then convert texts to unix timestamps
		if (!is_int($time1)) {
		  $time1 = strtotime($time1);
		}
		if (!is_int($time2)) {
		  $time2 = strtotime($time2);
		}
	 
		// If time1 is bigger than time2
		// Then swap time1 and time2
		if ($time1 > $time2) {
		  $ttime = $time1;
		  $time1 = $time2;
		  $time2 = $ttime;
		}
	 
		// Set up intervals and diffs arrays
		$intervals = array('year','month','day','hour','minute','second');
		$diffs = array();
	 
		// Loop thru all intervals
		foreach ($intervals as $interval) {
		  // Create temp time from time1 and interval
		  $ttime = strtotime('+1 ' . $interval, $time1);
		  // Set initial values
		  $add = 1;
		  $looped = 0;
		  // Loop until temp time is smaller than time2
		  while ($time2 >= $ttime) {
			// Create new temp time from time1 and interval
			$add++;
			$ttime = strtotime("+" . $add . " " . $interval, $time1);
			$looped++;
		  }
	 
		  $time1 = strtotime("+" . $looped . " " . $interval, $time1);
		  $diffs[$interval] = $looped;
		}
		return $diffs;
		/* $count = 0;
		$times = array();
		// Loop thru all diffs
		foreach ($diffs as $interval => $value) {
		  // Break if we have needed precission
		  if ($count >= $precision) {
		break;
		  }
		  // Add value and interval 
		  // if value is bigger than 0
		  if ($value > 0) {
		// Add s if value is not 1
		if ($value != 1) {
		  $interval .= "s";
		}
		// Add value and interval to times array
		$times[] = $value . " " . $interval;
		$count++;
		  }
		}
	 
		// Return string with times
		return implode(", ", $times); */
	}
	public function convertPtype($ptype){

		switch($ptype)
		{
		case 1:
		$img = '<img src="catalog/view/theme/default/image/b.gif" alt="b" height="16">&nbsp;';
		return $img;
		case 2:
		$img = '<img src="catalog/view/theme/default/image/m.gif" alt="m" height="16">&nbsp;';
		return $img;
		case 3:
		$img = '<img src="catalog/view/theme/default/image/cht.gif" alt="cht" height="16">&nbsp;';
		return $img;
		case 4:
		$img = '<img src="catalog/view/theme/default/image/ct.gif" alt="ct" height="16">&nbsp;';
		return $img;
		case 5:
		$img = '<img src="catalog/view/theme/default/image/tn.gif" alt="tn" height="16">&nbsp;';
		return $img;
		case 6;
		$img = '<img src="catalog/view/theme/default/image/tv.gif" alt="tv" height="16">&nbsp;';
		return $img;
		case 7;
		$img = '<img src="catalog/view/theme/default/image/k.gif" alt="k" height="16">&nbsp;';
		return $img;
		}
	}
	//
	public function index() {

		//tuan added title
		$this->document->setTitle($this->config->get('config_title'));
		
		$this->language->load('product/special');
		
		$this->data['text_default'] = $this->language->get('text_default');
		
		$this->load->model('catalog/category');
		
		$this->load->model('catalog/product');
		
		$this->load->model('tool/image'); 
		
		$this->load->model('localisation/zone');

		$results = $this->model_localisation_zone->getZonesByCountryId(230);
			
		$country_hash = array();
		
		foreach($results as $result){
			$country_hash[$result['zone_id']] = $result['name'];
		}
			
		if (isset($this->request->get['filter'])) {
			$filter = $this->request->get['filter'];
		} else {
			$filter = '';
		}
				
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			//$sort = 'p.sort_order';
			$sort = 'p.date_modified';
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			//$order = 'ASC';
			$order = 'DESC';
		}
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else { 
			$page = 1;
		}	
		//tuan country	
		if (isset($this->request->get['zone_id'])) {
			$zone_id = $this->request->get['zone_id'];
		} else {
			$zone_id = '';
		}
		
		if (isset($this->request->get['ptype'])) {
			$ptype = $this->request->get['ptype'];
		} else {
			$ptype = '';
		}
		
		if (isset($this->request->get['mobile'])) {
			$mobile = $this->request->get['mobile'];
		} else {
			$mobile = '';
		}
		
		//
		if (isset($this->request->get['limit'])) {
			$limit = $this->request->get['limit'];
		} else {
			$limit = $this->config->get('config_catalog_limit');
		}
		
		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),
       		'separator' => false
   		);	
			
		if (isset($this->request->get['path'])) {
			$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}	
			//tuan country
			if (isset($this->request->get['zone_id'])) {
				$url .= '&zone_id=' . $this->request->get['zone_id'];
			}
			
			if (isset($this->request->get['ptype'])) {
				$url .= '&ptype=' . $this->request->get['ptype'];
			}
			//
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}

		} else {
			$category_id = 0;
		}
		$category_id = 0;	
		$category_info = $this->model_catalog_category->getCategory($category_id);
		
		//tuan change to TRUE for list all products
		if (true) {
			

			$this->data['heading_title'] = $this->language->get('heading_title');
			$this->document->addScript('catalog/view/javascript/jquery/jquery.total-storage.min.js');
			$this->data['text_refine'] = $this->language->get('text_refine');
			$this->data['text_empty'] = $this->language->get('text_empty');			
			$this->data['text_quantity'] = $this->language->get('text_quantity');
			$this->data['text_manufacturer'] = $this->language->get('text_manufacturer');
			$this->data['text_model'] = $this->language->get('text_model');
			$this->data['text_price'] = $this->language->get('text_price');
			$this->data['text_tax'] = $this->language->get('text_tax');
			$this->data['text_points'] = $this->language->get('text_points');
			$this->data['text_compare'] = sprintf($this->language->get('text_compare'), (isset($this->session->data['compare']) ? count($this->session->data['compare']) : 0));
			$this->data['text_display'] = $this->language->get('text_display');
			$this->data['text_list'] = $this->language->get('text_list');
			$this->data['text_grid'] = $this->language->get('text_grid');
			$this->data['text_sort'] = $this->language->get('text_sort');
			$this->data['text_limit'] = $this->language->get('text_limit');
			$this->data['text_find_posts'] = $this->language->get('text_find_posts');
			
			$this->data['button_cart'] = $this->language->get('button_cart');
			$this->data['button_wishlist'] = $this->language->get('button_wishlist');
			$this->data['button_compare'] = $this->language->get('button_compare');
			$this->data['button_continue'] = $this->language->get('button_continue');
			
			// Set the last category breadcrumb		
			$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}	
			
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			//tuan country
			if (isset($this->request->get['zone_id'])) {
				$url .= '&zone_id=' . $this->request->get['zone_id'];
			}
			
			if (isset($this->request->get['ptype'])) {
				$url .= '&ptype=' . $this->request->get['ptype'];
			}
			//
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
									
			$this->data['compare'] = $this->url->link('product/compare');
			
			$url = '';
			
			if (isset($this->request->get['filter'])) {
				$url .= '&filter=' . $this->request->get['filter'];
			}	
						
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}	
			//tuan country
			if (isset($this->request->get['zone_id'])) {
				$url .= '&zone_id=' . $this->request->get['zone_id'];
			}
			
			if (isset($this->request->get['ptype'])) {
				$url .= '&ptype=' . $this->request->get['ptype'];
			}
			//
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
				
			$this->data['categories'] = array();
			
			$results = $this->model_catalog_category->getCategories($category_id);
			
			foreach ($results as $result) {
				$data = array(
					'filter_category_id'  => $result['category_id'],
					'filter_sub_category' => true
				);
				
				$product_total = $this->model_catalog_product->getTotalProducts($data);				
				if(isset($this->request->get['path'])) {
					$this->data['categories'][] = array(
						'name'  => $result['name'] . ($category_id == 0 ? '' : ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : '')),
						'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url)
					);
				}
				else {
					$this->data['categories'][] = array(
						'name'  => $result['name'] . ($category_id == 0 ? '' : ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : '')),
						'href'  => $this->url->link('product/special', 'path=' . 'this' . '_' . $result['category_id'] . $url)
					);
				}
			}
			
			$this->data['products'] = array();
			
			$data = array(
				'filter_category_id' => $category_id,
				'filter_filter'      => $filter, 
				'sort'               => $sort,
				'order'              => $order,
				'start'              => ($page - 1) * $limit,
				'limit'              => $limit,
				'zone_id'		 	=> $zone_id,
				'ptype'		 		=> $ptype,
				'mobile'		 		=> $mobile
			);
			
			$product_total = $this->model_catalog_product->getTotalProductSpecials($data);
			
			$results = $this->model_catalog_product->getProductSpecials($data);
			
			foreach ($results as $result) {

				if ($result['image']) {
					$image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
				} else {
					//$image = false;
					$image = $this->model_tool_image->resize('no_image.jpg', $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
				}
				
				//tuan
				if ($result['zone_id']) {
					$country_name = $country_hash[$result['zone_id']];
				} else {
					$country_name = false;
				}
				
				if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
					$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
				} else {
					$price = false;
				}
				
				if ((float)$result['special']) {
					$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
				} else {
					$special = false;
				}	
				
				if ($this->config->get('config_tax')) {
					$tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price']);
				} else {
					$tax = false;
				}				
				
				if ($this->config->get('config_review_status')) {
					$rating = (int)$result['rating'];
				} else {
					$rating = false;
				}
				
				//tuan add for date
				$is_new = 0;
				$date = new DateTime($result['date_modified'], new DateTimeZone('Asia/Bangkok'));
				$now = new DateTime();
				
				$days_diff = $this->dateDiff($date->format('d-m-Y H:i'), $now->format('d-m-Y H:i'));
					
				if($days_diff['day'] == 0){
				
					$date_diff = $days_diff['hour'] .' '.$this->language->get('text_hours').' '. $days_diff['minute'] .' '.$this->language->get('text_mins');
					$is_new = 1;
				} else if($days_diff['day'] < 4){
					$date_diff = $days_diff['day'].' '.$this->language->get('text_days').' '. $days_diff['hour'] .' '.$this->language->get('text_hours');
					$is_new = 1;
				} else if($days_diff['day'] >= 4 && $days_diff['day'] < 31){
					$date_diff = $days_diff['day'] .' '.$this->language->get('text_days');
				} else {
					$date_diff = $date->format('d-m-Y');
				}

				if(isset($this->request->get['path'])) {				
					$this->data['products'][] = array(
						'product_id'  => $result['product_id'],
						'thumb'       => $image,
						'name'        => $result['name'],
						'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
						'price'       => $price,
						'special'     => $special,
						'tax'         => $tax,
						'rating'      => $result['rating'],
						'country_name'=> $country_name,
						'reviews'     => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
						'href'        => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url),
						'date_diff'	  => $date_diff,
						'is_new'	  => $is_new,
						'ptype'	  	  => $ptype,
						'text_ptype'  => $this->convertPtype($result['ptype']),
						'mobile_text' => $result['mobile'],
						'mobile_href' => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&mobile=' . $result['mobile'])
					);
				} else {
					$this->data['products'][] = array(
						'product_id'  => $result['product_id'],
						'thumb'       => $image,
						'name'        => $result['name'],
						'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
						'price'       => $price,
						'special'     => $special,
						'tax'         => $tax,
						'rating'      => $result['rating'],
						'country_name'=> $country_name,
						'reviews'     => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
						'href'        => $this->url->link('product/product', 'path=' . 'this' . '&product_id=' . $result['product_id'] . $url),
						'date_diff'	  => $date_diff,
						'is_new'	  => $is_new,
						'ptype'	  	  => $ptype,
						'text_ptype'  => $this->convertPtype($result['ptype']),
						'mobile_text' => $result['mobile'],
						'mobile_href' => $this->url->link('product/special', 'path=' . 'this' . '&mobile=' . $result['mobile'])
					);
				}
			}
			//tuan add ptype
			$url = '';
			
			if (isset($this->request->get['filter'])) {
				$url .= '&filter=' . $this->request->get['filter'];
			}
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			if (isset($this->request->get['zone_id'])) {
				$url .= '&zone_id=' . $this->request->get['zone_id'];
			}
			
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}		
			
			$this->data['ptypes'] = array();
			//1: sell 
			//2: buy
			//$_['text_hire']//3
			//$_['text_needhire']    //4
			//$_['text_person']         //5
			//$_['text_work']          //6
			//$_['text_na']          //7
			if(isset($this->request->get['path'])) {
				$this->data['ptypes'][] = array(
					'text'  => $this->language->get('text_ptype_default'),
					'value' => '-1',
					'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&sort=p.date_modified&order=DESC' . $url)
				);
				$ptype = 1;
				
				$this->data['ptypes'][] = array(
					'text'  => $this->language->get('text_sell'),
					'value' => $ptype ,
					'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&ptype=' . $ptype . $url)
				);
				
				$ptype = 2;
				
				$this->data['ptypes'][] = array(
					'text'  => $this->language->get('text_buy'),
					'value' => $ptype ,
					'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&ptype=' . $ptype . $url)
				);	
				
				$ptype = 3;
				
				$this->data['ptypes'][] = array(
					'text'  => $this->language->get('text_hire'),
					'value' => $ptype ,
					'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&ptype=' . $ptype . $url)
				);	
				
				$ptype = 4;
				
				$this->data['ptypes'][] = array(
					'text'  => $this->language->get('text_needhire'),
					'value' => $ptype ,
					'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&ptype=' . $ptype . $url)
				);
				
				$ptype = 5;
				
				$this->data['ptypes'][] = array(
					'text'  => $this->language->get('text_person'),
					'value' => $ptype ,
					'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&ptype=' . $ptype . $url)
				);
				
				$ptype = 6;
				
				$this->data['ptypes'][] = array(
					'text'  => $this->language->get('text_work'),
					'value' => $ptype ,
					'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&ptype=' . $ptype . $url)
				);
				
				$ptype = 7;
				
				$this->data['ptypes'][] = array(
					'text'  => $this->language->get('text_na'),
					'value' => $ptype ,
					'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&ptype=' . $ptype . $url)
				);
				
			} else {
				$this->data['ptypes'][] = array(
					'text'  => $this->language->get('text_ptype_default'),
					'value' => 'p.date_modified-DESC',
					'href'  => $this->url->link('product/special', 'path=' . 'this'. '&sort=p.date_modified&order=DESC' . $url)
				);
				
				$ptype = 1;
				
				$this->data['ptypes'][] = array(
					'text'  => $this->language->get('text_sell'),
					'value' => $ptype ,
					'href'  => $this->url->link('product/special', 'path=' . 'this' . '&ptype=' . $ptype . $url)
				);
				
				$ptype = 2;
				
				$this->data['ptypes'][] = array(
					'text'  => $this->language->get('text_buy'),
					'value' => $ptype ,
					'href'  => $this->url->link('product/special', 'path=' . 'this' . '&ptype=' . $ptype . $url)
				);	
				
				$ptype = 3;
				
				$this->data['ptypes'][] = array(
					'text'  => $this->language->get('text_hire'),
					'value' => $ptype ,
					'href'  => $this->url->link('product/special', 'path=' . 'this' . '&ptype=' . $ptype . $url)
				);	
				
				$ptype = 4;
				
				$this->data['ptypes'][] = array(
					'text'  => $this->language->get('text_needhire'),
					'value' => $ptype ,
					'href'  => $this->url->link('product/special', 'path=' . 'this' . '&ptype=' . $ptype . $url)
				);
				
				$ptype = 5;
				
				$this->data['ptypes'][] = array(
					'text'  => $this->language->get('text_person'),
					'value' => $ptype ,
					'href'  => $this->url->link('product/special', 'path=' . 'this' . '&ptype=' . $ptype . $url)
				);
				
				$ptype = 6;
				
				$this->data['ptypes'][] = array(
					'text'  => $this->language->get('text_work'),
					'value' => $ptype ,
					'href'  => $this->url->link('product/special', 'path=' . 'this' . '&ptype=' . $ptype . $url)
				);
				
				$ptype = 7;
				
				$this->data['ptypes'][] = array(
					'text'  => $this->language->get('text_na'),
					'value' => $ptype ,
					'href'  => $this->url->link('product/special', 'path=' . 'this' . '&ptype=' . $ptype . $url)
				);
			}

			//
			$url = '';
			
			if (isset($this->request->get['filter'])) {
				$url .= '&filter=' . $this->request->get['filter'];
			}
			//tuan country
			if (isset($this->request->get['zone_id'])) {
				$url .= '&zone_id=' . $this->request->get['zone_id'];
			}
			
			if (isset($this->request->get['ptype'])) {
				$url .= '&ptype=' . $this->request->get['ptype'];
			}
			//
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
										
			$this->data['sorts'] = array();
			
			if(isset($this->request->get['path'])) {
				//tuan
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_default'),
					'value' => 'p.date_modified-DESC',
					'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&sort=p.date_modified&order=DESC' . $url)
				);
			//
				/* $this->data['sorts'][] = array(
					'text'  => $this->language->get('text_name_asc'),
					'value' => 'pd.name-ASC',
					'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&sort=pd.name&order=ASC' . $url)
				);

				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_name_desc'),
					'value' => 'pd.name-DESC',
					'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&sort=pd.name&order=DESC' . $url)
				); */

				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_price_asc'),
					'value' => 'p.price-ASC',
					'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&sort=p.price&order=ASC' . $url)
				); 

				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_price_desc'),
					'value' => 'p.price-DESC',
					'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&sort=p.price&order=DESC' . $url)
				); 
				
				if ($this->config->get('config_review_status')) {
					$this->data['sorts'][] = array(
						'text'  => $this->language->get('text_rating_desc'),
						'value' => 'rating-DESC',
						'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&sort=rating&order=DESC' . $url)
					); 
					
					$this->data['sorts'][] = array(
						'text'  => $this->language->get('text_rating_asc'),
						'value' => 'rating-ASC',
						'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&sort=rating&order=ASC' . $url)
					);
				}
				
				/* $this->data['sorts'][] = array(
					'text'  => $this->language->get('text_model_asc'),
					'value' => 'p.model-ASC',
					'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&sort=p.model&order=ASC' . $url)
				);

				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_model_desc'),
					'value' => 'p.model-DESC',
					'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . '&sort=p.model&order=DESC' . $url)
				); */
			} else {
				//tuan
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_default'),
					'value' => 'p.date_modified-DESC',
					'href'  => $this->url->link('product/special', 'path=' . 'this' . '&sort=p.date_modified&order=DESC' . $url)
				);
			//
				
			
				/* $this->data['sorts'][] = array(
					'text'  => $this->language->get('text_name_asc'),
					'value' => 'pd.name-ASC',
					'href'  => $this->url->link('product/special', 'path=' . 'this'. '&sort=pd.name&order=ASC' . $url)
				);

				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_name_desc'),
					'value' => 'pd.name-DESC',
					'href'  => $this->url->link('product/special', 'path=' . 'this'. '&sort=pd.name&order=DESC' . $url)
				); */

				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_price_asc'),
					'value' => 'p.price-ASC',
					'href'  => $this->url->link('product/special', 'path=' . 'this'. '&sort=p.price&order=ASC' . $url)
				); 

				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_price_desc'),
					'value' => 'p.price-DESC',
					'href'  => $this->url->link('product/special', 'path=' . 'this' . '&sort=p.price&order=DESC' . $url)
				); 
				
				if ($this->config->get('config_review_status')) {
					$this->data['sorts'][] = array(
						'text'  => $this->language->get('text_rating_desc'),
						'value' => 'rating-DESC',
						'href'  => $this->url->link('product/special', 'path=' . 'this' . '&sort=rating&order=DESC' . $url)
					); 
					
					$this->data['sorts'][] = array(
						'text'  => $this->language->get('text_rating_asc'),
						'value' => 'rating-ASC',
						'href'  => $this->url->link('product/special', 'path=' . 'this' . '&sort=rating&order=ASC' . $url)
					);
				}
				
				/* $this->data['sorts'][] = array(
					'text'  => $this->language->get('text_model_asc'),
					'value' => 'p.model-ASC',
					'href'  => $this->url->link('product/special', 'path=' . 'this' . '&sort=p.model&order=ASC' . $url)
				);

				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_model_desc'),
					'value' => 'p.model-DESC',
					'href'  => $this->url->link('product/special', 'path=' . 'this' . '&sort=p.model&order=DESC' . $url)
				); */
			}	
			
			$url = '';
			
			if (isset($this->request->get['filter'])) {
				$url .= '&filter=' . $this->request->get['filter'];
			}
				
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			//tuan country
			if (isset($this->request->get['zone_id'])) {
				$url .= '&zone_id=' . $this->request->get['zone_id'];
			}
			
			if (isset($this->request->get['ptype'])) {
				$url .= '&ptype=' . $this->request->get['ptype'];
			}
			//			
			
			$this->data['limits'] = array();
	
			$limits = array_unique(array($this->config->get('config_catalog_limit'), 25, 50, 75, 100));
			
			sort($limits);
			
			if(isset($this->request->get['path'] )){
				foreach($limits as $limits){
					$this->data['limits'][] = array(
						'text'  => $limits,
						'value' => $limits,
						'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . $url . '&limit=' . $limits)
					);
				}
			} else {
				foreach($limits as $limits){
					$this->data['limits'][] = array(
						'text'  => $limits,
						'value' => $limits,
						'href'  => $this->url->link('product/special', 'path=' . 'this' . $url . '&limit=' . $limits)
					);
				}
			}
			
			$url = '';
			
			if (isset($this->request->get['filter'])) {
				$url .= '&filter=' . $this->request->get['filter'];
			}
			
			if (isset($this->request->get['ptype'])) {
				$url .= '&ptype=' . $this->request->get['ptype'];
			}
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			$url1 = '';
			if (isset($this->request->get['limit'])) {
				$url1 = '&limit=' . $this->request->get['limit'];
			}		

			//tuan country	

			$results = $this->model_localisation_zone->getZonesByCountryId(230);

			if (isset($this->request->get['zone_id'])) {
				$this->data['zone_id'] = $this->request->get['zone_id'];
			} else {
				$this->data['zone_id'] = '';
			}

			$this->data['countries'] = array();

			if(isset($this->request->get['path'] )){
				$this->data['countries'][] = array(
							'text'  => $this->language->get('text_zone_default'),
							'value' => '-1',
							'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . $url . $url1 )
						);
				foreach($results as $result){
					$this->data['countries'][] = array(
							'text'  => $result['name'],
							'value' => $result['zone_id'],
							'href'  => $this->url->link('product/special', 'path=' . $this->request->get['path'] . $url . '&zone_id=' . $result['zone_id'] . $url1 )
						);
				}
			} else {
				$this->data['countries'][] = array(
							'text'  => $this->language->get('text_zone_default'),
							'value' => '-1',
							'href'  => $this->url->link('product/special', 'path=' . 'this' . $url . $url1 )
						);
				foreach($results as $result){
					$this->data['countries'][] = array(
						'text'  => $result['name'],
						'value' => $result['zone_id'],
						'href'  => $this->url->link('product/special', 'path=' . 'this' . $url . '&zone_id=' . $result['zone_id'] . $url1)
					);
				}
			}
			//
			
			$url = '';
			
			if (isset($this->request->get['filter'])) {
				$url .= '&filter=' . $this->request->get['filter'];
			}
				
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
			
			if (isset($this->request->get['zone_id'])) {
				$url .= '&zone_id=' . $this->request->get['zone_id'];
			}
			
			if (isset($this->request->get['ptype'])) {
				$url .= '&ptype=' . $this->request->get['ptype'];
			}
			
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}		

			$pagination = new Pagination();
			$pagination->total = $product_total;
			$pagination->page = $page;
			$pagination->limit = $limit;
			$pagination->text = $this->language->get('text_pagination');
			if(isset($this->request->get['path'] )){
				$pagination->url = $this->url->link('product/special', 'path=' . $this->request->get['path'] . $url . '&page={page}');
			} else {
				$pagination->url = $this->url->link('product/special', 'path=' . 'this' . $url . '&page={page}');
			}
			$this->data['pagination'] = $pagination->render();
		
			$this->data['sort'] = $sort;
			$this->data['order'] = $order;
			$this->data['limit'] = $limit;
			if (isset($this->request->get['ptype'])) {
				$ptype = $this->request->get['ptype'];
			} else {
				$ptype = -1;
			}
			$this->data['ptype'] = $ptype;
			
			if(isset($this->session->data['last_page'])){
				$this->data['continue'] = $this->session->data['last_page'];
			}else{
				$this->data['continue'] = $this->url->link('common/home', '', 'SSL');
			}

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/special.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/product/special.tpl';
			} else {
				$this->template = 'default/template/product/special.tpl';
			}
			
			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
			);
				
			$this->response->setOutput($this->render());										
    	} else {
			$url = '';
			
			if (isset($this->request->get['path'])) {
				$url .= '&path=' . $this->request->get['path'];
			}
			
			if (isset($this->request->get['filter'])) {
				$url .= '&filter=' . $this->request->get['filter'];
			}
												
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
				
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			//tuan country
			if (isset($this->request->get['zone_id'])) {
				$url .= '&zone_id=' . $this->request->get['zone_id'];
			}
			
			if (isset($this->request->get['ptype'])) {
				$url .= '&ptype=' . $this->request->get['ptype'];
			}
			//
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
						
			$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_error'),
				'href'      => $this->url->link('product/special', $url),
				'separator' => $this->language->get('text_separator')
			);
				
			$this->document->setTitle($this->language->get('text_error'));

      		$this->data['heading_title'] = $this->language->get('text_error');

      		$this->data['text_error'] = $this->language->get('text_error');

      		$this->data['button_continue'] = $this->language->get('button_continue');

      		if(isset($this->session->data['last_page'])){
				$this->data['continue'] = $this->session->data['last_page'];
			}else{
				$this->data['continue'] = $this->url->link('common/home', '', 'SSL');
			}
			
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} else {
				$this->template = 'default/template/error/not_found.tpl';
			}
			
			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
			);
					
			$this->response->setOutput($this->render());
		}
  	}
}
?>